<?php
class ChitieukenhsController extends AppController {

	var $name = 'Chitieukenhs';

	var $uses = array('Chitieukenh','Kehoach','Kehoachnv','Chitieunv');
	var $helpers =array('Ctable');
	
	function index() {
		$this->Chitieukenh->recursive = 0;
		$this->set('chitieukenhs', $this->paginate());
	}

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid chitieukenh', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('chitieukenh', $this->Chitieukenh->read(null, $id));
	}

	function add() {
		if (!empty($this->data)) {
			$this->Chitieukenh->create();
			if ($this->Chitieukenh->save($this->data)) {
				$this->Session->setFlash(__('The chitieukenh has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The chitieukenh could not be saved. Please, try again.', true));
			}
		}
		$kehoaches = $this->Chitieukenh->Kehoach->find('list');
		$phankenhs = $this->Chitieukenh->Phankenh->find('list');
		$hanghoas = $this->Chitieukenh->Hanghoa->find('list');
		$this->set(compact('kehoaches', 'phankenhs', 'hanghoas'));
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid chitieukenh', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->Chitieukenh->save($this->data)) {
				$this->Session->setFlash(__('The chitieukenh has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The chitieukenh could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Chitieukenh->read(null, $id);
		}
		$kehoaches = $this->Chitieukenh->Kehoach->find('list');
		$phankenhs = $this->Chitieukenh->Phankenh->find('list');
		$hanghoas = $this->Chitieukenh->Hanghoa->find('list');
		$this->set(compact('kehoaches', 'phankenhs', 'hanghoas'));
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for chitieukenh', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Chitieukenh->delete($id)) {
			$this->Session->setFlash(__('Chitieukenh deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Chitieukenh was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}

	function themchitieu($kh_id=null)
	{
		if(!$kh_id)
			$this->redirect(array('controller'=>'pages','action'=>'home'));
		
		if(!empty($this->data))
		{
			// luu kehoachnv
			$kh = $this->Kehoach->find('all',array('recursive'=>-1,'conditions'=>array('id'=>$kh_id)));
			$khnv['Kehoachnv']['thoigian_id']=$kh[0]['Kehoach']['thoigian_id']; 
			$khnv['Kehoachnv']['nhanvien_id']=$this->Session->read('cur_manv');
			$khnv['Kehoachnv']['ngaybd']=$kh[0]['Kehoach']['ngaybd'];
			$khnv['Kehoachnv']['ngaykt']=$kh[0]['Kehoach']['ngaykt'];
			$this->Kehoachnv->create();
			if(!$this->Kehoachnv->save($khnv))
			{
				$this->Session->setFlash(__('Không thể lưu. Vui lòng thử lại', true));
				$this->redirect(array('action' => 'themchitieu',$kh_id));
			}
			// luu chitieu kenh
			$colnum = $this->data['Chitieukenh']['colnum'];
			$rownum = $this->data['Chitieukenh']['rownum'];
			for($i=0;$i<$colnum;$i++)
	             for($j=0;$j<$rownum;$j++)
	             {
	             		$record['Chitieukenh']['kehoachnv_id']=$this->Kehoachnv->id;
	                    $record['Chitieukenh']['phankenh_id']=$this->data['Chitieukenh']['row'][$j];
	                    $record['Chitieukenh']['hanghoa_id']=$this->data['Chitieukenh']['col'][$i];
	                    if ($this->data['Chitieukenh'][$j][$i]!=NULL)
	                    	$record['Chitieukenh']['soluong']=$this->data['Chitieukenh'][$j][$i];
	                    else
	                    	$record['Chitieukenh']['soluong']=0;
	                    if(!$this->Chitieukenh->saveAll($record))
	                    {
	                         $this->Session->setFlash(__('Lưu Thất Bại', true));                     
	             		}
	    		}
	    		$this->Session->setFlash(__('Đã Lập Chỉ Tiêu Cho Các Kênh', true));
	       		$this->redirect(array('action' => 'hienchitieu',$this->Kehoachnv->id));
		}
		
		/*
		 * kiem tra
		 */
		$kehoach = $this->Kehoach->find('all',array('conditions'=>array('Kehoach.id'=>$kh_id)));
		$khnv = $this->Kehoachnv->find('all',array('conditions'=>array('Kehoachnv.thoigian_id'=>$kehoach[0]['Kehoach']['thoigian_id'],
																	'nhanvien_id'=>$this->Session->read('cur_manv'))));
		if(!empty($khnv))
		{
			$this->Session->setFlash(__('Chỉ tiêu cho kênh đã có', true));
			$this->redirect(array('action'=>'hienchitieu',$khnv[0]['Kehoachnv']['id']));
		}
		/*
		 * lay thong tin tu trung tam
		 */
		
		$chitieunvs = $this->Chitieunv->find('list',array('recursive'=>0,
											'conditions'=>array('kehoach_id'=>$kh_id,'Chitieunv.nhanvien_id'=>$this->Session->read('cur_manv')),
											'fields'=>array('Hanghoa.tenhh','Chitieunv.soluong','Nhanvien.tennv')));
		
		if (count($chitieunvs)!=0)
		{
			$soluong = array();
			$col_ctnv =array_keys($chitieunvs);
			$col_ctnv[]="Tổng";
			$row_ctnv =Null;
			if (count($col_ctnv)!=0)
			{
				$row_ctnv = array_keys($chitieunvs[$col_ctnv[0]]);
				$soluong = $chitieunvs[$col_ctnv[0]];
			}
			for ($j=0;$j<count($row_ctnv);$j++)
			{
				$tong=0;
				for ($i=0;$i<count($col_ctnv)-1;$i++)
				{
					$datanv[$j][$i]= $chitieunvs[$col_ctnv[$i]][$row_ctnv[$j]];
					$tong +=$datanv[$j][$i];		
				}
				$datanv[$j][$i]=$tong;
			}
		}
		
		$this->set(compact('soluong','col_ctnv', 'row_ctnv','datanv','thoigian','kehoach_id','dagiao'));
		
		/*
		 * lay kenh  va hang hoa dc giao
		 */
		$hanghoas = $this->Chitieunv->find('list',array(
										'recursive'=>0,
										'conditions'=>array('kehoach_id'=>$kh_id),
										'fields'=>array('hanghoa_id','Hanghoa.tenhh')));
		$kenhs = $this->Chitieukenh->Phankenh->find('list',array(
													'recursive'=>0,
													'conditions'=>array('nhanvien_id'=>$this->Session->read('cur_manv'),
																		'ngaykt_hd'=>null),
													'fields'=>array('Kenh.tenkenh')));

		$this->set(compact('hanghoas','kenhs','kh_id'));
	}

	function hienchitieu($kehoach_id=null)
	{
		if (!$kehoach_id)
			$this->redirect(array('controller'=>'pages','action'=>'home'));
			
		$kehoach = $this->Chitieukenh->Kehoachnv->find('list',array('conditions'=>array('Kehoachnv.id'=>$kehoach_id),
														'fields'=>array('thoigian_id','nhanvien_id','id')));
		if(!empty($kehoach))
		{
			$thoigian_id=key($kehoach[$kehoach_id]);
			$nhanvien_id =$kehoach[$kehoach_id][$thoigian_id]; 
			///Ke hoach cua nhan vien
			$thoigian = $this->Chitieukenh->Kehoachnv->Thoigian->find('list',array('conditions'=>array('id'=>$thoigian_id),'fields'=>array('thoigian')));
			$chitieunvs = $this->Chitieukenh->find('list',array('recursive'=>0,
												'conditions'=>array('kehoachnv_id'=>$kehoach_id),
												'fields'=>array('Hanghoa.tenhh','Chitieukenh.soluong','Phankenh.kenh_id')));
			//debug($thoigian);
	        if (count($chitieunvs)!=0)
			{
				$col_ctnv =array_keys($chitieunvs);
				$kenhs = $this->Chitieukenh->Phankenh->find('list',array(
														'recursive'=>0,
														'conditions'=>array('nhanvien_id'=>$nhanvien_id,
																			'ngaykt_hd'=>null),
														'fields'=>array('kenh_id','Kenh.tenkenh')));
				$col_ctnv[]="<b>Tổng</b>";
				$row_ctnv =Null;
				if (count($col_ctnv)!=0)
				{
					$row_ctnv = array_keys($chitieunvs[$col_ctnv[0]]);
				}
				for ($j=0;$j<count($row_ctnv);$j++)
				{
					$tong=0;
					for ($i=0;$i<count($col_ctnv)-1;$i++)
					{
						$datanv[$i][$j]= $chitieunvs[$col_ctnv[$i]][$row_ctnv[$j]];
						$tong +=$datanv[$i][$j];		
					}
					$datanv[$i][$j]=$tong;
				}
				foreach ($col_ctnv as $key => $value)
					if($value!='<b>Tổng</b>')
						$col_ctnv[$key] = $kenhs[$value];
			}
		}
		$this->set(compact('col_ctnv', 'row_ctnv','datanv','thoigian','kehoach_id','dagiao'));
	}

	function suachitieu($kehoach_id=null)
	{
		if(!$kehoach_id)
			$this->redirect(array('controller'=>'pages','action'=>'home'));

		if(!empty($this->data))
		{
			
			$colnum = $this->data['Chitieukenh']['colnum'];
			$rownum = $this->data['Chitieukenh']['rownum'];			
			for($i=0;$i<$colnum;$i++)
	             for($j=0;$j<$rownum;$j++)
	             {
	             		$record['Chitieukenh']['id'] = $this->data['Chitieukenh']['id'][$j][$i];
	             		$record['Chitieukenh']['kehoachnv_id']=$kehoach_id;
	                    $record['Chitieukenh']['phankenh_id']=$this->data['Chitieukenh']['row'][$j];
	                    $record['Chitieukenh']['hanghoa_id']=$this->data['Chitieukenh']['col'][$i];
	                    if ($this->data['Chitieukenh'][$j][$i]!=NULL)
	                    	$record['Chitieukenh']['soluong']=$this->data['Chitieukenh'][$j][$i];
	                    else
	                    	$record['Chitieukenh']['soluong']=0;
	                    if(!$this->Chitieukenh->saveAll($record))
	                    {
	                         $this->Session->setFlash(__('Lưu Thất Bại', true));                     
	             		}
	    		}
	    		$this->Session->setFlash(__('Đã Lập Kế Hoạch Cho Các Kênh', true));
	       		$this->redirect(array('action' => 'hienchitieu',$kehoach_id));
		}
		$kh = $this->Kehoachnv->find('all',array('recursive'=>-1,'conditions'=>array('id'=>$kehoach_id)));
		$nhanvien_id = $kh[0]['Kehoachnv']['nhanvien_id'];
		$chitieunvs = $this->Chitieukenh->find('list',array(
											'recursive'=>0,
											'conditions'=>array('kehoachnv_id'=>$kehoach_id),
											'fields'=>array('Hanghoa.tenhh','Chitieukenh.soluong','Phankenh.kenh_id')));
		$chitieunvs_id = $this->Chitieukenh->find('list',array(
											'recursive'=>0,
											'conditions'=>array('kehoachnv_id'=>$kehoach_id),
											'fields'=>array('Hanghoa.tenhh','Chitieukenh.id','Phankenh.kenh_id')));
		$col_kenhs = $this->Chitieukenh->find('list',array(
												'recursive'=>0,
												'conditions'=>array('kehoachnv_id'=>$kehoach_id),
												'fields'=>array('Chitieukenh.phankenh_id','Phankenh.kenh_id')));
		$row_ctnv = $this->Chitieukenh->find('list',array(
												'recursive'=>0,
												'conditions'=>array('kehoachnv_id'=>$kehoach_id),
												'fields'=>array('Chitieukenh.hanghoa_id','Hanghoa.tenhh')));
		
		$kenhs = $this->Chitieukenh->Phankenh->find('list',array(
													'recursive'=>0,
													'conditions'=>array('nhanvien_id'=>$nhanvien_id,
																		'ngaykt_hd'=>null),
													'fields'=>array('kenh_id','Kenh.tenkenh')));
		$cot=0;
		$dong=0;
		foreach ($col_kenhs as $id_nv=>$valuenv)
		{	
			$dong=0;
			foreach ($row_ctnv as $id_hh=>$valuehh)
			{
				$datanvsl[$cot][$dong] = $chitieunvs[$col_kenhs[$id_nv]][$row_ctnv[$id_hh]];
				$datanvid[$cot][$dong] = $chitieunvs_id[$col_kenhs[$id_nv]][$row_ctnv[$id_hh]];
				$dong++;
				}
			$cot++;
		}
		foreach ($col_kenhs as $key => $value)
			$col_kenhs[$key] = $kenhs[$value];
	
		$this->set(compact('kehoach_id','col_kenhs','row_ctnv','datanvsl','datanvid'));
	}

	function capnhatchitieu($kehoach_id=null)
	{
		if(!$kehoach_id)
			$this->redirect(array('controller'=>'pages','action'=>'home'));
		
		if(!empty($this->data))
		{
			$colnum = $this->data['Chitieukenh']['colnum'];
			$rownum = $this->data['Chitieukenh']['rownum'];
			for($i=0;$i<$colnum;$i++)
	             for($j=0;$j<$rownum;$j++)
	             {
	             		$record['Chitieukenh']['kehoachnv_id']=$kehoach_id;
	                    $record['Chitieukenh']['phankenh_id']=$this->data['Chitieukenh']['col'][$i];
	                    $record['Chitieukenh']['hanghoa_id']=$this->data['Chitieukenh']['row'][$j];
	                    if ($this->data['Chitieukenh'][$j][$i]!=NULL)
	                    	$record['Chitieukenh']['soluong']=$this->data['Chitieukenh'][$j][$i];
	                    else
	                    	$record['Chitieukenh']['soluong']=0;
	                    if(!$this->Chitieukenh->saveAll($record))
	                    {
	                         $this->Session->setFlash(__('Lưu Thất Bại', true));                     
	             		}
	    		}
	    		$this->Session->setFlash(__('Đã Cập Nhật Chỉ Tiêu Cho Các Kênh', true));
	       		$this->redirect(array('action' => 'hienchitieu',$kehoach_id));
		}
		
		/*
		 * lay thong tin tu trung tam
		 */
		$khnv = $this->Kehoachnv->find('all',array('recursive'=>-1,'conditions'=>array('Kehoachnv.id'=>$kehoach_id)));
		$thoigian_id = $khnv[0]['Kehoachnv']['thoigian_id'];
		$kehoach = $this->Kehoach->find('all',array('recursive'=>-1,'conditions'=>array('Kehoach.thoigian_id'=>$thoigian_id,'Kehoach.donvi_id'=>$this->Session->read('cur_dv'))));
		$kh_id = $kehoach['0']['Kehoach']['id'];
		
		$hh = $this->Chitieukenh->find('list',array(
												'conditions'=>array('kehoachnv_id'=>$kehoach_id),
												'fields'=>array('Chitieukenh.hanghoa_id'),
												'group'=>array('Chitieukenh.hanghoa_id')));

		$chitieunvs = $this->Chitieunv->find('list',array('recursive'=>0,
											'conditions'=>array('kehoach_id'=>$kh_id,'Chitieunv.nhanvien_id'=>$this->Session->read('cur_manv')
											//,'NOT'=>array('hanghoa_id'=>$hh)
											),'fields'=>array('Hanghoa.tenhh','Chitieunv.soluong','Nhanvien.tennv')));
		if (count($chitieunvs)!=0)
		{
			$soluong = array();
			$col_ctnv =array_keys($chitieunvs);
			$col_ctnv[]="Tổng";
			$row_ctnv =Null;
			if (count($col_ctnv)!=0)
			{
				$row_ctnv = array_keys($chitieunvs[$col_ctnv[0]]);
				$soluong = $chitieunvs[$col_ctnv[0]];
			}
			for ($j=0;$j<count($row_ctnv);$j++)
			{
				$tong=0;
				for ($i=0;$i<count($col_ctnv)-1;$i++)
				{
					$datanv[$j][$i]= $chitieunvs[$col_ctnv[$i]][$row_ctnv[$j]];
					$tong +=$datanv[$j][$i];		
				}
				$datanv[$j][$i]=$tong;
			}
		}
		
		$this->set(compact('soluong','col_ctnv', 'row_ctnv','datanv','thoigian','kehoach_id','dagiao'));
		
		/*
		 * lay kenh  va hang hoa dc giao
		 */
		$kehoach = $this->Kehoach->find('all',array('conditions'=>array('Kehoach.id'=>$kh_id)));
		$hanghoas = $this->Chitieunv->find('list',array(
										'recursive'=>0,
										'conditions'=>array('kehoach_id'=>$kh_id,'NOT'=>array('hanghoa_id'=>$hh)),
										'fields'=>array('hanghoa_id','Hanghoa.tenhh')));
		$kenhs = $this->Chitieukenh->Phankenh->find('list',array(
													'recursive'=>0,
													'conditions'=>array('nhanvien_id'=>$this->Session->read('cur_manv'),
																		'ngaykt_hd'=>null),
													'fields'=>array('Kenh.tenkenh')));
		
		$this->set(compact('hanghoas','kenhs','kh_id'));
	}

}
